package com.newmuis.open.codeeval.easy;

/**
 * CodeEval.com > Easy Challenges > Prime Palindrome
 * 
 * Description:
 * 
 * Write a program to determine the biggest prime palindrome under 1000.
 * 
 * Input sample:
 * 
 * None
 * 
 * Output sample:
 * 
 * Your program should print the largest palindrome on stdout. i.e.
 * 
 * 929
 * 
 * @author Jonathan Newmuis (jon@newmuis.com)
 * @category easy
 * 
 */
public class prime_palindrome {
	/**
	 * Determines whether a String is a palindrome.
	 * 
	 * @param str the String to determine whether it is a palindrome.
	 * @return true if and only if the specified String {@code str} is a palindrome.
	 */
	private static boolean isPalindrome(final String str) {
		final int length = str.length();
		final int midpoint = length / 2;

		for (int i = 0; i < midpoint; i++) {
			if (str.charAt(i) != str.charAt(length - 1 - i)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Determines whether an integer is prime.
	 * 
	 * @param integer the integer to determine whether it is prime.
	 * @return true if and only if the specified integer {@code x} is prime.
	 */
	private static boolean isPrime(final int x) {
		boolean prime = true;
		for (int i = 2; i < x; i++) {
			if (x % i == 0) {
				prime = false;
			}
		}
		return prime;
	}

	/**
	 * @param args No arguments should be specified. Any specified arguments are ignored.
	 */
	public static void main(final String[] args) {
		int maxPalindrome = 0;

		// Calculate the sum of the first 1000 prime integers
		for (int i = 2; i < 1000; i++) {
			if (isPrime(i) && isPalindrome(Integer.toString(i))) {
				maxPalindrome = i;
			}
		}

		// Output the sum.
		System.out.println(maxPalindrome);
	}
}
